package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author with葡萄成熟时
 * @version 1.0
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * @description: 添加购物车
     * @param:
     * @return:
     * @author Wait葡萄成熟时
     * @date: 下午9:20 2024/11/14
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //先把用传过来的信息和用户的信息接收一下
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //从jwt获得用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //查询购物车是否有这些
        //这也不可能传回来多条啊，为什么要用List
        List<ShoppingCart> list= shoppingCartMapper.list(shoppingCart);
        //有的话就修改表数量加一，
        if(list!=null&&list.size()>0){
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartMapper.updateNumberById(shoppingCart1);

        }else {//没有就添加进表
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId!=null){//传进来是菜品id情况下
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());

            }else{//传进来是套餐id情况下
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.selectSetmealById(setmealId);
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());


            }
            //不管是啥都插入表
                shoppingCartMapper.insert(shoppingCart);

        }




    }
    /**
     * @description: 查看购物车
     * @param:
     * @return:
     * @author Wait葡萄成熟时
     * @date: 下午9:33 2024/11/14
     */
    @Override
    public List<ShoppingCart> listShoppingCart() {

        List<ShoppingCart>  list=shoppingCartMapper.listShoppingCart();
        return list;
    }

    /**
     * @description: 删除购物车中一个商品
     * @param:
     * @return:
     * @author Wait葡萄成熟时
     * @date: 下午9:49 2024/11/14
     */
    @Override
    public void deleteShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //必须带着用户的id，不然表会混乱
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        shoppingCartMapper.deleteByDishIdOrSetmealId(shoppingCart);
    }

    /**
     * @description: 清空购物车
     * @param:
     * @return:
     * @author Wait葡萄成熟时
     * @date: 下午9:52 2024/11/14
     */
    @Override
    public void cleanAll() {

        shoppingCartMapper.cleanAll();
    }
}
